const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsType: ['水果', '蔬菜'],
    goodsInfoList: [{
      wayBillNumber: '', // 运单号
      goods_Name: '', // 货物名称
      goodsType:{
        id:'',
        typeName:'',
        typeCode:''
      }, // 货物类别
      nums: '', // 件数
      long: '', // 长
      width: '', // 宽
      height: '', // 高
      volume: '', // 体积
      volumeWeight: '', //体积重量
      realWeight: 0, //实际重量
      chargeableWeight: '', //计费重量
      price: 0, //单价
      discount: 1, //折扣
      freight: '', //运费
      goodsMoney: '', //货款金额
      insuredValue:''//投保价值
    }],
    isshops:false,
    partnerNames:[],
    firstshop:''
  },
  onLoad(){//搜素货物名称
  var that=this
    var company = wx.getStorageSync("COMPANY")
    var tokenL = app.globalData.token
    wx.request({
      url: app.globalData.apiServer + "/api/GoodsType/SearchForSelectControl",
      data: {
        // company: {
        //   id: company.id,
        //   companyName: company.companyName
        // },

      },
      method: "POST",
      header: {
        'content-type': 'application/json',
        "Authorization": "Bearer " + tokenL,
        "LoginType": 4
      },
      success: function (res) {
        console.log(res)
        if (res.data.responseCode == 1000) {
          var serviceList = []
          that.setData({
            partnerNames:res.data.obj
          })
          
          console.log(that.data.partnerNames)
        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'loading',
            duration: 2000
          });
        }

      }

    })

   },

  bindGoodsDanhaoTxt: function (e) {
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].wayBillNumber': e.detail.value
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].wayBillNumber': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].wayBillNumber': e.detail.value
      })
    }
  },

  bindGoodsNameTxt: function (e) {
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].goods_Name': e.detail.value
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].goods_Name': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].goods_Name': e.detail.value
      })
    }
  },

  bindGoodsTypeTxt: function (e) {
    console.log(e)
    console.log(e.currentTarget.dataset.idx)

    var that = this
    that.setData({
      isshops: !that.data.isshops
    })



    // let idx = e.currentTarget.dataset.idx
    // if (idx === 0) {
    //   this.setData({
    //     'goodsInfoList[0].type': e.detail.value
    //   })
    // } else if (idx === 1) {
    //   this.setData({
    //     'goodsInfoList[1].type': e.detail.value
    //   })
    // } else if (idx === 2) {
    //   this.setData({
    //     'goodsInfoList[2].type': e.detail.value
    //   })
    // }
  },
   itemshop(e) {//点击选择货物
     console.log(e)
    var that = this
    that.setData({
      'goodsInfoList[0].goodsType.typeName': e.currentTarget.dataset.name,
      'goodsInfoList[0].goodsType.id': e.currentTarget.dataset.pid,
      'goodsInfoList[0].goodsType.typeCode': e.currentTarget.dataset.typecode
    })
  },

  // bindPickerChange: function (e) {
  //   let idx = e.currentTarget.dataset.idx
  //   if (idx === 0) {
  //     this.setData({
  //       'goodsInfoList[0].type': this.data.goodsType[e.detail.value]
  //     })
  //   } else if (idx === 1) {
  //     this.setData({
  //       'goodsInfoList[1].type': this.data.goodsType[e.detail.value]
  //     })
  //   } else if (idx === 2) {
  //     this.setData({
  //       'goodsInfoList[2].type': this.data.goodsType[e.detail.value]
  //     })
  //   }
  // },

  bindGoodsNumTxt: function (e) {
    var that =this
    console.log(e,"e")
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].nums': e.detail.value
      })
      var volume = (that.data.goodsInfoList[0].long * that.data.goodsInfoList[0].width * that.data.goodsInfoList[0].height / 1000000 * that.data.goodsInfoList[0].nums).toFixed(2)
      if (volume != NaN) {
        that.setData({
          'goodsInfoList[0].volume': volume
        })
      }
      var chargeableWeight = 0
      if (Number(that.data.goodsInfoList[0].realWeight) > Number(that.data.goodsInfoList[0].volumeWeight)) {
        chargeableWeight = that.data.goodsInfoList[0].realWeight
      } else {
        chargeableWeight = that.data.goodsInfoList[0].volumeWeight
      }   
      this.setData({
        'goodsInfoList[0].chargeableWeight': chargeableWeight
      }) 
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].nums': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].nums': e.detail.value
      })
    }
  },

  bindGoodsLengthTxt: function (e) {
    var that =this
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].long': e.detail.value
      })
      var volume = (that.data.goodsInfoList[0].long * that.data.goodsInfoList[0].width * that.data.goodsInfoList[0].height / 1000000 * that.data.goodsInfoList[0].nums).toFixed(2)
      if (volume != NaN){
        that.setData({
          'goodsInfoList[0].volume': volume
        })
      }
      var chargeableWeight = 0
      if (Number(that.data.goodsInfoList[0].realWeight) > Number(that.data.goodsInfoList[0].volumeWeight)) {
        chargeableWeight = that.data.goodsInfoList[0].realWeight
      } else {
        chargeableWeight = that.data.goodsInfoList[0].volumeWeight
      }   
      this.setData({
        'goodsInfoList[0].chargeableWeight': chargeableWeight
      }) 
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].long': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].long': e.detail.value
      })
    }
  },

  bindGoodsWidthTxt: function (e) {
    var that = this
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].width': e.detail.value
      })
      var volume = (that.data.goodsInfoList[0].long * that.data.goodsInfoList[0].width * that.data.goodsInfoList[0].height / 1000000 * that.data.goodsInfoList[0].nums).toFixed(2)
      if (volume != NaN) {
        that.setData({
          'goodsInfoList[0].volume': volume
        })
      }
      var chargeableWeight = 0
      if (Number(that.data.goodsInfoList[0].realWeight) > Number(that.data.goodsInfoList[0].volumeWeight)) {
        chargeableWeight = that.data.goodsInfoList[0].realWeight
      } else {
        chargeableWeight = that.data.goodsInfoList[0].volumeWeight
      }   
      this.setData({
        'goodsInfoList[0].chargeableWeight': chargeableWeight
      }) 
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].width': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].width': e.detail.value
      })
    }
  },

  bindGoodsHeightTxt: function (e) {
    var that = this
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].height': e.detail.value
      })
      var volume = (that.data.goodsInfoList[0].long * that.data.goodsInfoList[0].width * that.data.goodsInfoList[0].height / 1000000 * that.data.goodsInfoList[0].nums).toFixed(2)
      if (volume != NaN) {
        that.setData({
          'goodsInfoList[0].volume': volume
        })
      }
      var volumeWeight = (that.data.goodsInfoList[0].volume * 1000000 / 6000).toFixed(2)
        if (volumeWeight != NaN) {
          this.setData({
            'goodsInfoList[0].volumeWeight': volumeWeight
          })

        }
        var chargeableWeight = 0
        if (Number(that.data.goodsInfoList[0].realWeight) > Number(that.data.goodsInfoList[0].volumeWeight)) {
          chargeableWeight = that.data.goodsInfoList[0].realWeight
        } else {
          chargeableWeight = that.data.goodsInfoList[0].volumeWeight
        } 
        this.setData({
          'goodsInfoList[0].chargeableWeight': chargeableWeight
        })   
        var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
        that.setData({
          'goodsInfoList[0].freight': freight
        })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].height': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].height': e.detail.value
      })
    }
  },

  bindGoodsVolumeTxt: function (e) {
    var that =this
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      
      this.setData({
        'goodsInfoList[0].volume': e.detail.value
      })
      var volumeWeight = (that.data.goodsInfoList[0].volume * 1000000 / 6000).toFixed(2)
      if (volumeWeight != NaN) {
        this.setData({
          'goodsInfoList[0].volumeWeight': volumeWeight
        })
      }
      var chargeableWeight = 0
      if (Number(that.data.goodsInfoList[0].realWeight) > Number(that.data.goodsInfoList[0].volumeWeight)) {
        chargeableWeight = that.data.goodsInfoList[0].realWeight
      } else {
        chargeableWeight = that.data.goodsInfoList[0].volumeWeight
      }
      this.setData({
        'goodsInfoList[0].chargeableWeight': chargeableWeight
      })  
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].volume': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].volume': e.detail.value
      })
    }
  },

  bindGoodsTjWeightTxt: function (e) {
    var that =this
    let idx = e.currentTarget.dataset.idx
    var volumeWeight = that.data.goodsInfoList[0].volume*1000000/6000
    var chargeableWeight = 0
    if (idx === 0) {
      if (volumeWeight != NaN){
        this.setData({
          'goodsInfoList[0].volumeWeight': volumeWeight
        })
      }
      if (Number(that.data.goodsInfoList[0].realWeight) > Number(that.data.goodsInfoList[0].volumeWeight)) {
        chargeableWeight = that.data.goodsInfoList[0].realWeight
      }else{
        chargeableWeight = that.data.goodsInfoList[0].volumeWeight
      }     
      this.setData({
        'goodsInfoList[0].chargeableWeight': chargeableWeight
      }) 
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].volumeWeight': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].volumeWeight': e.detail.value
      })
    }
  },

  bindGoodsSjWeightTxt: function (e) {
    var that =this
    let idx = e.currentTarget.dataset.idx
    var chargeableWeight = 0
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].realWeight': e.detail.value
      })
      if (Number(that.data.goodsInfoList[0].realWeight) > Number(that.data.goodsInfoList[0].volumeWeight)){
        chargeableWeight = that.data.goodsInfoList[0].realWeight 
      }else{
        chargeableWeight = that.data.goodsInfoList[0].volumeWeight
      }
      this.setData({
        'goodsInfoList[0].chargeableWeight': chargeableWeight
      })     
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].realWeight': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].realWeight': e.detail.value
      })
    }
  },

  bindGoodsJfWeightTxt: function (e) {
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      
      if (goodsInfoList[0].realWeight){}
      this.setData({
        'goodsInfoList[0].chargeableWeight': e.detail.value
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].chargeableWeight': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].chargeableWeight': e.detail.value
      })
    }
  },

  bindGoodsUnitPriceTxt: function (e) {
    var that = this
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].price': e.detail.value
      })
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].price': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].price': e.detail.value
      })
    }
  },

  bindGoodsDiscountTxt: function (e) {
    var that =this
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].discount': e.detail.value
      })
      var freight = that.data.goodsInfoList[0].price * that.data.goodsInfoList[0].chargeableWeight * that.data.goodsInfoList[0].discount
      that.setData({
        'goodsInfoList[0].freight': freight
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].discount': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].discount': e.detail.value
      })
    }
  },

  bindGoodsFreightTxt: function (e) {
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].freight': e.detail.value
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].freight': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].freight': e.detail.value
      })
    }
  },

  bindGoodsAmountTxt: function (e) {
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].goodsMoney': e.detail.value
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].goodsMoney': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].goodsMoney': e.detail.value
      })
    }
  },
  bindinsuredValueTxt(e){
    let idx = e.currentTarget.dataset.idx
    if (idx === 0) {
      this.setData({
        'goodsInfoList[0].insuredValue': e.detail.value
      })
    } else if (idx === 1) {
      this.setData({
        'goodsInfoList[1].insuredValue': e.detail.value
      })
    } else if (idx === 2) {
      this.setData({
        'goodsInfoList[2].insuredValue': e.detail.value
      })
    }
  },

  bindAddGoodsItem() {
    let that = this;
    let goodsItem = [{
      wayBillNumber: '', // 运单号
      goods_Name: '', // 货物名称
      type: '', // 货物类别
      nums: '', // 件数
      long: '', // 长
      width: '', // 宽
      height: '', // 高
      volume: '', // 体积
      volumeWeight: '', //体积重量
      realWeight: '', //实际重量
      chargeableWeight: '', //计费重量
      price: '', //单价
      discount: '', //折扣
      freight: '', //运费
      goodsMoney: '', //货款金额
    }];

    that.setData({
      goodsInfoList: that.data.goodsInfoList.concat(goodsItem)
    });
  },

  bindDelGoodsItem(e) {
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    console.log(idx)
    wx.showToast({
      title: '正在删除',
      icon: 'loading',
      duration: 500
    });
    that.data.goodsInfoList.splice(idx, 1)
    setTimeout(function () {
      that.setData({
        goodsInfoList: that.data.goodsInfoList
      });
    }, 500);
  },

  bindGoodsInfo() {
    var that = this
    var goodsInfoList = that.data.goodsInfoList
    for (var i = 0; i < goodsInfoList.length; i++) {
      // if (goodsInfoList[i].wayBillNumber == '') {
      //   wx.showToast({
      //     title: '请输入运单号',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } else 
      if (goodsInfoList[i].goods_Name == '') {
        wx.showToast({
          title: '请输入货物名称',
          icon: 'loading',
          duration: 2000
        });
        return false
      } else if (goodsInfoList[i].type == '') {
        wx.showToast({
          title: '请输入货物类别',
          icon: 'loading',
          duration: 2000
        });
        return false
      } else if (goodsInfoList[i].nums == '') {
        wx.showToast({
          title: '请输入货物件数',
          icon: 'loading',
          duration: 2000
        });
        return false
      } 
      // else if (goodsInfoList[i].long == '') {
      //   wx.showToast({
      //     title: '请输入货物长度',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].width == '') {
      //   wx.showToast({
      //     title: '请输入货物宽度',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].height == '') {
      //   wx.showToast({
      //     title: '请输入货物高度',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      else if (goodsInfoList[i].volume == '') {
        wx.showToast({
          title: '请输入货物体积',
          icon: 'loading',
          duration: 2000
        });
        return false
      } else if (goodsInfoList[i].volumeWeight == '') {
        wx.showToast({
          title: '请输入体积重量',
          icon: 'loading',
          duration: 2000
        });
        return false
      } 
      // else if (goodsInfoList[i].realWeight == '') {
      //   wx.showToast({
      //     title: '请输入实际重量',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].chargeableWeight == '') {
      //   wx.showToast({
      //     title: '请输入计费重量',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].price == '') {
      //   wx.showToast({
      //     title: '请输入单价',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].discount == '') {
      //   wx.showToast({
      //     title: '请输入折扣',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].freight == '') {
      //   wx.showToast({
      //     title: '请输入运费',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].goodsMoney == '') {
      //   wx.showToast({
      //     title: '请输入货物金额',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // } 
      // else if (goodsInfoList[i].insuredValue == '') {
      //   wx.showToast({
      //     title: '请输入投保价值',
      //     icon: 'loading',
      //     duration: 2000
      //   });
      //   return false
      // }
    }
    // console.log(that.data.goodsInfoList,"---111")
    // var item = JSON.stringify(that.data.goodsInfoList);
    getApp().globalData.items = that.data.goodsInfoList
    // console.log(getApp().globalData.items,"8888")
    wx.navigateBack()

  }
})